package com.xueyu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xueyu.dao.Community1Dao;
import com.xueyu.dao.CommunityDao;
import com.xueyu.domain.Products.ProductsBean;
import com.xueyu.domain.community.CommunityBean;
import com.xueyu.util.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Community")
public class CommunityController {
    @Autowired
    Community1Dao communityDao;
    @RequestMapping("/add")
    public Map<String, Object> add(@RequestBody CommunityBean CommunityBean)throws Exception{
        Map<String, Object> map = new HashMap<>();
        String communityName = CommunityBean.getCommunityName();
        int communityId = CommunityBean.getCommunityId();
        QueryWrapper<CommunityBean> wrapper = new QueryWrapper<>();
        wrapper.eq("communityName", communityName);

        if (communityId == 0) {
            int count = Math.toIntExact(communityDao.selectCount(wrapper));
            if (count > 0) {
                map.put("code", 201);
                map.put("msg", "请不要添重复的分类！");
                return map;
            } else {
                int result = communityDao.insert(CommunityBean);
                if (result != 0) {
                    map.put("code", 200);
                    System.out.println("true row" + result);
                } else {
                    System.out.println("Error");
                }
            }
        }else {
            CommunityBean oldBean = communityDao.selectById(communityId);
            if (oldBean == null) {
                map.put("code", 201);
                map.put("msg", "分类不存在！");
            } else {
                int count = Math.toIntExact(communityDao.selectCount(wrapper));
                if (count > 0 && !communityName.equals(oldBean.getCommunityName())) {
                    map.put("code", 201);
                    map.put("msg", "请不要添重复的分类！");
                } else {
                    int result = communityDao.updateById(CommunityBean);
                    if (result != 0) {
                        map.put("code", 200);
                        System.out.println("true");
                    } else {
                        System.out.println("false");
                    }
                }
            }
        }
        return map;
    }

    @RequestMapping("/del/{CommunityId}")
    public ResultBean del(@PathVariable("CommunityId") int CommunityId){
        communityDao.deleteById(CommunityId);
        return new ResultBean();
    }
    @RequestMapping("/getid/{CommunityId}")
    public Map<String, Object> getid(@PathVariable("CommunityId") int CommunityId){
        ResultBean resultBean = new ResultBean();
        if (CommunityId>0){
            QueryWrapper<CommunityBean> queryWrapper = Wrappers.query();
            queryWrapper.eq("CommunityId",CommunityId);
            List<CommunityBean> CommuntiyList = communityDao.selectList(queryWrapper);
            resultBean.put("data",CommuntiyList);
        }
        return resultBean;
    }
    @RequestMapping("/fy")
    public ResultBean fy(@RequestBody Map map){
        int current = (Integer)map.get("Current");
        int size = (Integer)map.get("size");
        Page<CommunityBean>page = new Page<>(current, size);
        QueryWrapper<CommunityBean> queryWrapper = createSchoolQueryWrapper(map);
        page = communityDao.selectPage(page,queryWrapper);
        List<CommunityBean>list = page.getRecords();
        ResultBean resultBean = new ResultBean();
        resultBean.put("page",page);
        resultBean.put("list",list);
        return resultBean;
    }
    @RequestMapping("/zd")
    public ResultBean zd(@RequestBody Map map){
        QueryWrapper<CommunityBean> queryWrapper = createSchoolQueryWrapper(map);
        List<CommunityBean> list = communityDao.selectList(queryWrapper);
        ResultBean resultBean = new ResultBean();
        resultBean.put("list",list);
        return resultBean;
    }
    private QueryWrapper<CommunityBean> createSchoolQueryWrapper(Map map) {
        String CommunityId = (String) map.get("CommunityId");
        String communityName = (String) map.get("communityName");
        QueryWrapper<CommunityBean> queryWrapper = Wrappers.query();
        if (CommunityId != null && !"".equals(CommunityId)) {
            queryWrapper.eq("CommunityId", CommunityId);
        }
        if (communityName != null && !"".equals(communityName)) {
            queryWrapper.like("communityName", "%" + communityName + "%");
        }

        return queryWrapper;
    }
}
